package com.xianghualin.service.impl;

import com.xianghualin.mapper.BoardMapper;
import com.xianghualin.mapper.PostMapper;
import com.xianghualin.pojo.Board;
import com.xianghualin.pojo.Post;
import com.xianghualin.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * PostServiceImpl
 */
@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private BoardMapper boardMapper;

    @Override
    public void addPostByPost(Post post) {
        postMapper.addPost(post);
    }

    @Override
    public Post listPostContent(int postId) {
        return postMapper.findPostByPostId(postId);
    }

    @Override
    public List<Post> listAllPost() {
        return postMapper.listAllPostInfo();
    }

    @Override
    public void deletePost(int postId) {
        // 更新Board数据
        int boardId = postMapper.findPostByPostId(postId).getPostBoardId();
        Board board = boardMapper.findBoardByBoardId(boardId);
        board.setBoardPostNum(board.getBoardPostNum() - 1);
        boardMapper.updateBoardByBoard(board);

        // 删除post
        postMapper.deletePostById(postId);
    }
}
